﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Premiere.DTO;
using System.Collections;
using Premiere.Window;
using System.Data;

namespace PremiereMX
{
    public class PartCategoryLogic
    {
        PartCategoryDTO m_subject = new PartCategoryDTO();
        IMainForm m_mainForm;
        IUserControlPartCategory m_form;
        EditMode m_editMode = EditMode.OK;

        public PartCategoryLogic(IMainForm mainForm, IUserControlPartCategory form)
        {
            m_mainForm = mainForm;
            m_form = form;
        }
        public void Init()
        {
            updateForm();
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
            updatePartCategoryList();
        }
        private void updateForm()
        {
            m_form.Code = m_subject.Code;
            m_form.Named = m_subject.Name;
            m_form.LastUpdate = m_subject.LastUpdate;
            m_form.ComputerName = m_subject.ComputerName;
            m_form.UserUpdateBy = m_subject.UserName;
            m_form.Category1Value = m_subject.Category1Value;
            m_form.Category2Value = m_subject.Category2VAlue;
            m_form.Category3Value = m_subject.Category3Value;
        }
        private void SetButtonEditMode(EditMode editMode)
        {
            m_form.SetButtonEditMode(editMode);
            m_editMode = editMode;
        }
        private void updatePartCategoryList()
        {
            IList list = ServiceRegistry.GetInstance().GetPartCategoryService().LoadAll();
            m_form.UpdateList(list);
        }
        public void Edit()
        {
            m_form.SetToReadOnly(false);
            m_form.SetButtonEditMode(EditMode.Insert);
        }
        private void updateSubject()
        {
            m_subject.Code = m_form.Code;
            m_subject.ComputerName = Environment.MachineName;
            m_subject.Name = m_form.Named;
            m_subject.Note = string.Empty;
            m_subject.UserName = m_mainForm.GetUser().Code;
            m_subject.LastUpdate = DateTime.Now;
            m_subject.Category1Value = m_form.Category1Value;
            m_subject.Category2VAlue = m_form.Category2Value;
            m_subject.Category3Value = m_form.Category3Value;
        }
        internal void UpdateSubject(PartCategoryDTO curr)
        {
            m_subject = curr;
            m_form.ClearForm();
            updateForm();
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
        }
        public void Save()
        {
            try
            {
                if (m_form.IsValidSubject())
                {
                    updateSubject();
                    m_subject.Id = ServiceRegistry.GetInstance().GetPartCategoryService().Save(m_subject);
                    m_form.ShowMessage("Record has been saved!");
                    m_form.SetToReadOnly(true);
                    SetButtonEditMode(EditMode.OK);
                    m_form.UpdateList(new object[] { m_subject });
                }
            }
            catch (Exception ex)
            {
                m_form.ShowMessage(ex.Message);
            }
        }
        internal void Delete()
        {
            if (m_subject.Id == 0) return;
            ServiceRegistry.GetInstance().GetPartCategoryService().Delete(m_subject);
            m_form.RemoveItem(m_subject.Id);
            m_form.ClearForm();
        }
        internal void New()
        {
            m_subject = new PartCategoryDTO();
            m_form.ClearForm();
            SetButtonEditMode(EditMode.Insert);
            m_form.SetToReadOnly(false);
            m_form.FocusToCode();
        }
        public void Refresh()
        {
        }
        internal void Print()
        {
            DataSet ds = ServiceRegistry.GetInstance().GetPartCategoryService().GetPreviewReportData(m_subject.Id);
            m_mainForm.Print(ds);
        }
    }
}
